home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / cmdity / yk212src.lha / Yak_2.12_Src / icon.c < prev    next >
C/C++ Source or Header  |  1996-01-18  |  6KB  |  347 lines

  1. /*
  2.  *  Routines dealing with processing of Yak's icon
  3.  *  (be they for tooltypes or AppIcon purposes).
  4.  *  
  5.  */
  6. #include <proto/exec.h>
  7. #include <proto/dos.h>
  8. #include <proto/wb.h>
  9. #include <proto/icon.h>
  10.  
  11. #include <exec/types.h>
  12. #include <exec/ports.h>
  13. #include <exec/memory.h>
  14. #include <dos/dos.h>
  15. #include <workbench/startup.h>
  16. #include <workbench/workbench.h>
  17. #include <string.h>
  18.  
  19. #include "icon.h"
  20. #include "yak.h"
  21.  
  22. struct MsgPort *AppMsgPort=NULL;
  23. ULONG           appsigflag=0L;
  24.  
  25. static struct DiskObject *diskobj;
  26.  
  27. __regargs BOOL
  28.    GetYakIcon(STRPTR name)
  29. {
  30.    if (name)
  31.    {
  32.       /* Get Disk Object */
  33.       diskobj = GetDiskObject(name);
  34.    }
  35.    
  36.    return (BOOL)(diskobj ? TRUE : FALSE);
  37. }
  38.  
  39. /* can cope with multiple calls */
  40. void
  41.    FreeYakIcon()
  42. {
  43.    if (diskobj)
  44.    {
  45.       FreeDiskObject(diskobj);
  46.       diskobj = NULL;
  47.    }
  48. }
  49.  
  50. /* like ArgString() */
  51. __regargs char *
  52.    TTString(char *name, char *def)
  53. {
  54.    char *what;
  55.    if (diskobj)
  56.       if (what = FindToolType(diskobj->do_ToolTypes, name))
  57.          return what;
  58.    return def;
  59. }
  60.  
  61. /* like ArgInt() */
  62. __regargs LONG
  63.    TTInt(char *name, LONG def)
  64. {
  65.    char *what;
  66.    if (diskobj)
  67.       if (what = FindToolType(diskobj->do_ToolTypes, name))
  68.          StrToLong(what, &def);
  69.    return def;
  70. }
  71.  
  72. /* simple extension to ArgXXX routines */
  73. __regargs BOOL
  74.    TTBool(char *name, BOOL def)
  75. {
  76.    char    *s;
  77.    
  78.    s = TTString(name, def ? "YES" : "NO");
  79.    
  80.    return  (BOOL)(((strcmp(s, "YES") == 0) ||
  81.                    (strcmp(s, "TRUE") == 0) ||
  82.                    (strcmp(s, "ON") == 0)) ? TRUE : FALSE);
  83. }
  84.  
  85.  
  86. #ifndef PREFS
  87.  
  88. #define APPICONS_NUM 32
  89.  
  90. static struct 
  91. {
  92.     struct AppIcon *appicon;
  93.     struct Window  *window;
  94. } iconified[APPICONS_NUM];
  95.  
  96.  
  97. static BOOL
  98. CreateAppIconPort(void)
  99. {
  100.    if (AppMsgPort = CreateMsgPort())
  101.    {
  102.       appsigflag = 1 << AppMsgPort->mp_SigBit;
  103.       RegisterSignal(appsigflag);
  104.       return TRUE;
  105.    }
  106.    else
  107.       return FALSE;
  108. }
  109.  
  110.  
  111. /* create our AppIcon */
  112. BOOL
  113.    MakeYakAppIcon(void)
  114. {
  115.    if (diskobj != NULL)
  116.     {
  117.         if (CreateAppIconPort() == TRUE)
  118.         {
  119.             diskobj->do_CurrentX = TTInt("ICONXPOS", NO_ICON_POSITION);
  120.             diskobj->do_CurrentY = TTInt("ICONYPOS", NO_ICON_POSITION);
  121.             if (iconified[0].appicon = AddAppIconA(0, 0, TTString("ICONNAME", "Yak!"),
  122.                                                                 AppMsgPort, 0, diskobj, 0))
  123.             {
  124.                 return TRUE;
  125.             }
  126.             else DeleteMsgPort(AppMsgPort);
  127.         }
  128.     }
  129.    return FALSE;
  130. }
  131.  
  132. extern STRPTR ProgramName;
  133.  
  134. static char *
  135.    ChooseProgramName(struct Window *Window)
  136. {
  137.    char *name = NULL;
  138.    struct Task *WinTask;
  139.    STRPTR TaskName;
  140.       
  141.    if (Window->Flags & WFLG_WBENCHWINDOW)
  142.    {
  143.       /* It's probably a drawer */
  144.       name = "envarc:sys/def_drawer";
  145.    }
  146.    else
  147.    {
  148.       if ((WinTask = Window->UserPort->mp_SigTask) &&
  149.           (TaskName = WinTask->tc_Node.ln_Name))
  150.       {
  151.          /* Should be done by using patterns */
  152.          if (stricmp(TaskName,"KCON") == 0)
  153.          {
  154.             name = "sys:system/shell";
  155.          }
  156.          else
  157.          {
  158.             /* By default take Yak icon */
  159.             name = ProgramName;
  160.          }
  161.       }
  162.    }
  163.    return(name);
  164. }
  165.  
  166.  
  167. static char *
  168.    ChooseAppIconTitle(struct Window *Window)
  169. {
  170.    char *title = NULL;
  171.    
  172.    if (Window->Title)
  173.    {
  174.       title = Window->Title;
  175.    }
  176.    else
  177.    {
  178.       struct Task *WinTask;
  179.       STRPTR TaskName;
  180.       
  181.       if ((WinTask = Window->UserPort->mp_SigTask) &&
  182.           (TaskName = WinTask->tc_Node.ln_Name))
  183.       {
  184.          title = TaskName;
  185.       }
  186.       else
  187.       {
  188.          title = "Iconified window";
  189.       }
  190.    }
  191.    
  192.    return(title);
  193. }
  194.  
  195. /* create our AppIcon */
  196. __regargs BOOL
  197.    MakeAppIconified (struct Window *Window)
  198. {
  199.    int ID;
  200.    BOOL error = TRUE;
  201.  
  202.    for (ID=1; ID < APPICONS_NUM; ID++)
  203.    {
  204.         if (iconified[ID].window == Window)
  205.         {
  206.             /* This window has already been iconified 
  207.              * Don't do it twice !
  208.              */
  209.             return(FALSE);
  210.         }
  211.    }
  212.    
  213.     /* Find an unused appicon */
  214.     ID = 1;
  215.    while ((iconified[ID].appicon != NULL) &&
  216.              (ID < APPICONS_NUM))
  217.    {
  218.         ID++;
  219.    }
  220.    
  221.    if (ID == APPICONS_NUM)
  222.    {
  223.         /* No more memory left for iconification */
  224.         return(FALSE);
  225.    }
  226.  
  227.    GetYakIcon(ChooseProgramName(Window));
  228.  
  229.    if (diskobj == NULL)
  230.    {
  231.       /* Maybe the choosen program name doesn't exist so try
  232.        * once again with Yak icon
  233.        */
  234.       GetYakIcon(ProgramName);
  235.    }
  236.    
  237.    if (diskobj != NULL)
  238.    {
  239.         if (AppMsgPort == NULL)
  240.         {
  241.             /* Probably there's no appicon already created */
  242.             CreateAppIconPort();
  243.         }
  244.       
  245.         if (AppMsgPort != NULL)
  246.         {
  247.             if (iconified[ID].appicon = AddAppIconA(ID, 0,
  248.                                                  ChooseAppIconTitle(Window),
  249.                                                  AppMsgPort, 0L, diskobj, 0L))
  250.             {
  251.                 iconified[ID].window = Window;
  252.             }
  253.             else
  254.             { 
  255.                 error = FALSE;
  256.             }
  257.         }
  258.         FreeYakIcon();
  259.    }
  260.    else
  261.    { 
  262.       error = FALSE;
  263.    }
  264.     return(error);
  265. }
  266.  
  267. extern void RevealWindow(struct Window *window);
  268. extern BOOL ShowYakInterface(void);
  269.  
  270. /* bye bye icons... */
  271. void
  272.    RemoveAllAppIcons()
  273. {
  274.    int i;
  275.     struct Message *msg;
  276.  
  277.     /* Remove Yak appicon if any */
  278.     if (iconified[0].appicon != NULL)
  279.     {
  280.         RemoveAppIcon(iconified[0].appicon);
  281.         iconified[0].appicon = NULL;
  282.     }
  283.  
  284.    for (i=1; i<APPICONS_NUM; i++)
  285.    {
  286.       if (iconified[i].appicon != NULL)
  287.       {
  288.          RevealWindow(iconified[i].window);
  289.        RemoveAppIcon(iconified[i].appicon);
  290.          iconified[i].appicon = NULL;
  291.          iconified[i].window  = NULL;
  292.       }
  293.    }
  294.  
  295.    
  296.    if (AppMsgPort != NULL) 
  297.     {
  298.         /* Clear the port */
  299.         while (msg = GetMsg(AppMsgPort))
  300.             ReplyMsg(msg);
  301.  
  302.         DeleteMsgPort(AppMsgPort);
  303.     }
  304. }
  305.  
  306.  
  307.  
  308.  
  309. /* Process AppIcons messages if any */
  310. void
  311.    ProcessAppMsg(ULONG sigrcvd)
  312. {
  313.    struct AppMessage *appmsg;
  314.    ULONG              ID;
  315.  
  316.    if (sigrcvd & appsigflag)  
  317.    {
  318.       while (appmsg = (struct AppMessage *)GetMsg(AppMsgPort))
  319.       {
  320.          ID = appmsg->am_ID;
  321.          ReplyMsg((struct Message *)appmsg);
  322.  
  323.          if (ID == 0)
  324.          {
  325.             /* It's a message from Yak AppIcon */
  326.             ShowYakInterface();
  327.          }
  328.          else
  329.          {
  330.             /* It's a message from an AppIcon attached to an iconified window 
  331.              * So let's close our appicon and deiconify window
  332.              */
  333.             RemoveAppIcon(iconified[ID].appicon);
  334.             RevealWindow(iconified[ID].window);
  335.             iconified[ID].appicon = NULL;
  336.             iconified[ID].window  = NULL;
  337.          }
  338.       }
  339.    }
  340. }
  341.  
  342. #endif
  343.  
  344.  
  345.  
  346.  
  347.